package com.example.demo.queue;

import com.example.demo.Service.TOrderService;
import com.example.demo.Service.UserService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MessageListener {
    private UserService ce;
   private TOrderService to;
    @Autowired
    public void setCe(UserService ce) {
        this.ce = ce;
    }
@Autowired
    public void setTo(TOrderService to) {
        this.to = to;
    }
    //    @RabbitListener(bindings = {
//            @QueueBinding(
//                    exchange = @Exchange(name = "spring.boot.delay.exchange", type = "direct"),
//                    value = @Queue(name = "spring.boot.rabbit.queue"),
//                    key = {"spring.boot.rabbit.routingkey"}
//            )
//    })
//    public void listen(String msg) {
//        System.out.println("收到消息：" + msg);
//    }


    @RabbitListener(queues = {"spring.boot.dlx.queue"})
    public void delay(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody(),"utf-8");
        to.zhuangtai(msg);

    System.out.println("收到延时队列的消息：" + msg + ":");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
